package cn.minhx.domain.activity.service.trial.node;

import static cn.minhx.types.enums.ResponseCode.ILLEGAL_PARAMETER;

import cn.minhx.domain.activity.model.entity.MarketProductEntity;
import cn.minhx.domain.activity.model.entity.TrialBalanceEntity;
import cn.minhx.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.minhx.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.minhx.types.design.tree.StrategyHandler;
import cn.minhx.types.exception.AppException;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 根节点
 *
 * @author minhx
 */
@Slf4j
@Service
public class RootNode extends AbstractGroupBuyMarketSupport {

	@Resource
	private SwitchNode switchNode;

	@Override
	protected TrialBalanceEntity doApply(MarketProductEntity requestParameter,
			DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
		log.info("拼团商品查询试算服务-RootNode userId:{} requestParameter:{}", requestParameter.getUserId(),
				JSON.toJSONString(requestParameter));
		// 参数判断
		if (StringUtils.isBlank(requestParameter.getUserId()) || StringUtils.isBlank(requestParameter.getGoodsId())
				|| StringUtils.isBlank(requestParameter.getSource())
				|| StringUtils.isBlank(requestParameter.getChannel())) {
			throw new AppException(ILLEGAL_PARAMETER);
		}
		return router(requestParameter, dynamicContext);
	}

	@Override
	public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> next(
			MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) {
		return switchNode;
	}
}
